﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using RegexLib.Web.Models.Data;

namespace RegexLib.Web.Models.Web
{
	/// <summary>
	/// 表示web.config文件中的regexlib节。
	/// </summary>
	public class RxConfigurationSection : ConfigurationSection
	{
		/// <summary>
		/// 获取一个配置实例。
		/// </summary>
		public static RxConfigurationSection Instance
		{
			get { return (RxConfigurationSection)ConfigurationManager.GetSection("regexlib"); }
		}

		/// <summary>
		/// 获取xpus/mailServer节点下的配置。
		/// </summary>
		[ConfigurationProperty("mailServer")]
		public RxMailServerConfiguration MailServer
		{
			get { return (RxMailServerConfiguration)base["mailServer"]; }
		}

		[ConfigurationProperty("ipPolicy")]
		public RxIPPolicyCollection IPPolicy
		{
			get { return (RxIPPolicyCollection)base["ipPolicy"]; }
		}
	}

	#region mailServer
	/// <summary>
	/// 表示regexlib/mailServer节点下的配置。
	/// </summary>
	public class RxMailServerConfiguration : ConfigurationElement
	{
		/// <summary>
		/// 获取mailServer/host属性。
		/// </summary>
		[ConfigurationProperty("host", IsRequired = true)]
		public string Host
		{
			get
			{
				return (string)base["host"];
			}
		}

		/// <summary>
		/// 获取mailServer/port属性。
		/// </summary>
		[ConfigurationProperty("port")]
		public int Port
		{
			get
			{
				return (int)base["port"];
			}
		}

		/// <summary>
		/// 获取mailServer/enableSSL属性。
		/// </summary>
		[ConfigurationProperty("enableSSL")]
		public bool EnableSSL
		{
			get
			{
				return (bool)base["enableSSL"];
			}
		}

		/// <summary>
		/// 获取mailServer/userName属性。
		/// </summary>
		[ConfigurationProperty("userName")]
		public string UserName
		{
			get
			{
				return (string)base["userName"];
			}
		}

		/// <summary>
		/// 获取mailServer/password属性。
		/// </summary>
		[ConfigurationProperty("password")]
		public string Password
		{
			get
			{
				return (string)base["password"];
			}
		}

		/// <summary>
		/// 获取mailServer/address属性。
		/// </summary>
		[ConfigurationProperty("address")]
		public string Address
		{
			get
			{
				return (string)base["address"];
			}
		}

		/// <summary>
		/// 获取mailServer/encoding属性。
		/// </summary>
		[ConfigurationProperty("encoding")]
		public string Encoding
		{
			get
			{
				return (string)base["encoding"];
			}
		}
	}
	#endregion mailServer

	#region ipPolicy
	[ConfigurationCollection(typeof(RxIPPolicyItem), AddItemName = "policy", CollectionType = ConfigurationElementCollectionType.BasicMap)]
	public class RxIPPolicyCollection : ConfigurationElementCollection
	{
		protected override ConfigurationElement CreateNewElement()
		{
			return new RxIPPolicyItem();
		}

		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((RxIPPolicyItem)element).Type;
		}
	}

	public class RxIPPolicyItem : ConfigurationElement
	{
		[ConfigurationProperty("type", IsRequired = true)]
		public RxIPRecordType Type
		{
			get { return (RxIPRecordType)base["type"]; }
		}

		[ConfigurationProperty("pausePolicy", IsRequired = true)]
		public RxIPPausePolicy PausePolicy
		{
			get { return (RxIPPausePolicy)base["pausePolicy"]; }
		}

		[ConfigurationProperty("blockPolicy", IsRequired = true)]
		public RxIPBlockPolicy BlockPolicy
		{
			get { return (RxIPBlockPolicy)base["blockPolicy"]; }
		}
	}

	public class RxIPPausePolicy : ConfigurationElement
	{
		[ConfigurationProperty("duration", IsRequired = true)]
		public TimeSpan Duration
		{
			get { return (TimeSpan)base["duration"]; }
		}

		[ConfigurationProperty("records", IsRequired = true)]
		public int Records
		{
			get { return (int)base["records"]; }
		}
	}

	public class RxIPBlockPolicy : ConfigurationElement
	{
		[ConfigurationProperty("duration", IsRequired = true)]
		public TimeSpan Duration
		{
			get { return (TimeSpan)base["duration"]; }
		}

		[ConfigurationProperty("records", IsRequired = true)]
		public int Records
		{
			get { return (int)base["records"]; }
		}
	}
	#endregion ipPolicy
}
